package com.ljkj.cordial.chat.widget;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.ljkj.cordial.chat.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 单选
 */
public class SingleCommonDialog extends Dialog {
    private final Unbinder unbinder;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.listView)
    ListView listView;
    @BindView(R.id.tv_left_submit)
    TextView tvLeftSubmit;
    @BindView(R.id.tv_right_submit)
    TextView tvRightSubmit;
    @BindView(R.id.ll_bottom)
    LinearLayout llBottom;
    private SingleCommonAdapter singleCommonAdapter;
    private int selectedPosition;
    private String selectedValue;

    public int getSelectedPosition() {
        return selectedPosition;
    }

    public String getSelectedValue() {
        return selectedValue;
    }

    public SingleCommonDialog(@NonNull Context context) {
        super(context, com.nic.view.R.style.dialog_no_bg);
        setContentView(R.layout.dialog_single_common);
        unbinder = ButterKnife.bind(this);
        setCancelable(false);
        initLayout();
        singleCommonAdapter = new SingleCommonAdapter();
        listView.setAdapter(singleCommonAdapter);
        singleCommonAdapter.setItemClickListener(new SingleCommonAdapter.OnSingleItemClickListener() {
            @Override
            public void onClick(View view, int position, String value) {
                selectedPosition = position;
                selectedValue = value;
            }
        });
    }

    public SingleCommonDialog setTitle(String title) {
        tvTitle.setText(title);
        return this;
    }

    public SingleCommonDialog setSelected(int position) {
        singleCommonAdapter.setSelected(position);
        return this;
    }

    /**
     * 数据清理
     */
    public SingleCommonDialog setClearData() {
        String[] str = getContext().getResources().getStringArray(R.array.array_cyc_clear);
        List<String> list = new ArrayList<>();
        for (String s : str) {
            list.add(s);
        }
        singleCommonAdapter.setList(list);
        return this;
    }

    public SingleCommonDialog setReadFinishData() {
        String[] str = getContext().getResources().getStringArray(R.array.array_read_finish_clear);
        List<String> list = new ArrayList<>();
        for (String s : str) {
            list.add(s);
        }
        singleCommonAdapter.setList(list);
        return this;
    }

    public SingleCommonDialog setFontSizeData() {
        String[] str = getContext().getResources().getStringArray(R.array.array_font_size);
        List<String> list = new ArrayList<>();
        for (String s : str) {
            list.add(s);
        }
        singleCommonAdapter.setList(list);
        return this;
    }


    public SingleCommonDialog setSingleItems(List<String> items) {
        singleCommonAdapter.setList(items);
        return this;
    }

    public SingleCommonDialog setLeftSubmit(String content, final OnSingleItemClickListener listener) {
        tvLeftSubmit.setText(content);
        tvLeftSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (listener != null) {
                    listener.onClick(v, getSelectedPosition(), getSelectedValue());
                }

            }
        });
        return this;
    }

    public SingleCommonDialog setRightSubmit(String content, final OnSingleItemClickListener listener) {
        tvRightSubmit.setText(content);

        tvRightSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (listener != null) {
                    listener.onClick(v, getSelectedPosition(), getSelectedValue());
                }
            }
        });

        return this;
    }

    public void initLayout() {
//        if (ChatApp.getInstance().getFontScale() > ChatApp.getInstance().FONT_NORMAL) {
//            llBottom.setOrientation(LinearLayout.VERTICAL);
//            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tvRightSubmit.getLayoutParams();
//            params.leftMargin = 0;
//            int dp8 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, llBottom.getResources().getDisplayMetrics());
//            params.topMargin = dp8;
//        }

    }

    public SingleCommonDialog setRightSubmitGray(String content, OnSingleItemClickListener listener) {
//        tvRightSubmit.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.bt_gray_circle_angle));
//        tvRightSubmit.setTextColor(ContextCompat.getColor(getContext(), R.color.green5));
        tvRightSubmit.setText(content);
        tvRightSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (listener != null) {
                    listener.onClick(v, getSelectedPosition(), getSelectedValue());
                }
            }
        });

        return this;
    }

    public SingleCommonDialog setSingleSubmitGray(String content, final OnSingleItemClickListener listener) {
        tvLeftSubmit.setText(content);
        int dp96 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 120, getContext().getResources().getDisplayMetrics());
        int dp8 = getContext().getResources().getDimensionPixelOffset(R.dimen.dimen_8);
        tvLeftSubmit.setPadding(dp96, dp8, dp96, dp8);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tvLeftSubmit.getLayoutParams();
        params.leftMargin = dp8;
        params.rightMargin = dp8;
        tvLeftSubmit.setLayoutParams(params);

        tvLeftSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (listener != null) {
                    listener.onClick(v, getSelectedPosition(), getSelectedValue());
                }
            }
        });

        tvRightSubmit.setVisibility(View.GONE);
        return this;
    }


    @Override
    public void dismiss() {
        super.dismiss();
        unbinder.unbind();
    }

    public interface OnSingleItemClickListener {
        void onClick(View view, int position, String value);
    }
}
